package gfx;

import javax.microedition.khronos.opengles.GL10;

import util.GameVars;
import util.Logger;
import util.VertexBuffer;

import android.graphics.PointF;

public class GUI {
	private Image controlBox;
	private Image controlCircle;
	
	private PointF controlCenter;
	private PointF touchPoint;
	private PointF movement;
	
	private float[] controlBoxVertexes = new float[12];
	
	// TODO Implement isChanged to all the classes that could use it (to help limit unnecessary update calls)
	private boolean isChanged;
	private boolean isMoving;
	
	public GUI(){
		load((short) 1);
	}
	
	public void load(short layout){
		isMoving = false;
		
		touchPoint = new PointF(-1, -1);
		controlCenter = new PointF(3, 1.5f);
		movement = new PointF(0, 0);
		
		controlBox = new Image(GameVars.TEXTURE_CONTROL_BOX, 6, 3);
		controlCircle = new Image(GameVars.TEXTURE_CONTROL_CIRCLE, 2, 2);
	}
	
	public void update(long currentTime){
		
	}
	
	public void draw(GL10 gl){
		drawControlBox(gl);
	}
	
	private void drawControlBox(GL10 gl){
		// Standard Box Draw.
		// TODO Change 3 to a variable somewhere.
		controlBoxVertexes[0] = 0;
		controlBoxVertexes[1] = 0;
		controlBoxVertexes[2] = 0;
		controlBoxVertexes[3] = 0;
		controlBoxVertexes[4] = 3;
		controlBoxVertexes[5] = 0;
		controlBoxVertexes[6] = Camera.GL_WIDTH;
		controlBoxVertexes[7] = 0;
		controlBoxVertexes[8] = 0;
		controlBoxVertexes[9] = Camera.GL_WIDTH;
		controlBoxVertexes[10] = 3;
		controlBoxVertexes[11] = 0;
		
		gl.glPushMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(controlBoxVertexes));
		// TODO Change to a changeable setting.

		gl.glColor4f(0, 0, 0, 1);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, controlBoxVertexes.length / 3);
		
		gl.glColor4f(1, 1, 1, 1);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glPopMatrix();		

		// It doesn't fit in right now... I'll have to redraw it or just scrap it
//		controlBox.draw(gl, controlCenter);
		controlCircle.draw(gl, controlCenter);
	}
	
	public boolean isChanged(){ return isChanged; }

	public void handleUp(PointF location) {
		touchPoint.set(-1, -1);
		movement.set(0, 0);
		isMoving = false;
	}

	public void handleDrag(PointF location) {
		if(isMoving)
			// Old Implementation (Joystick-esque.  Felt Funny)
//			movement.set(Math.abs(location.x - touchPoint.x) > controlCircle.getWidth() * 0.5f ? Math.signum(location.x - touchPoint.x) * controlCircle.getWidth() * 0.5f : location.x - touchPoint.x, Math.abs(location.y - touchPoint.y) > controlCircle.getWidth() * 0.5f ? Math.signum(location.y - touchPoint.y) * controlCircle.getWidth() * 0.5f : location.y - touchPoint.y);
			movement.set(location.x - touchPoint.x, location.y - touchPoint.y);
	}

	public boolean handleTouch(PointF location) {
		if(Math.sqrt((location.x - controlCenter.x) * (location.x - controlCenter.x) + (location.y - controlCenter.y) * (location.y - controlCenter.y)) < controlCircle.getWidth() * 0.5f){
			touchPoint.set(location.x, location.y);
			isMoving = true;
			return true;
		}
		return false;
	}

	/** @return Returns the Movement, NOT the location to move to.  */
	public PointF getMovement(){ return movement; }
}
